package com.example.pei.ithinklearnt.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.bean.praises;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Pei on 2017/12/16.
 */

public class PraiseModelImp implements PraiseModel{
    @Override
    public void praiseAlumniCircle(final String userName, final String objectId, praiseAlumniCircle listener) {

        final SharedPreferences.Editor editor= AppApplication.getContext().getSharedPreferences(userName+"_praises", Context.MODE_PRIVATE).edit();
        SharedPreferences preferences=AppApplication.getContext().getSharedPreferences(userName+"_praises",Context.MODE_PRIVATE);

        if(preferences.getInt(objectId,0)==0){
            //未点赞状态变成点赞状态
            editor.putInt(objectId,1);
            editor.apply();
            listener.becomePraised();        //监听回调
            //调整服务器上的数据
            BmobQuery<praises> query = new BmobQuery<praises>();
            query.addWhereEqualTo("userID", userName);
            query.findObjects(new FindListener<praises>() {
                @Override
                public void done(final List<praises> object, BmobException e) {
                    if (e == null) {
                        if(object.size()==1){
                            //服务器存在这条，则修改，增加点赞数据
                            final praises sd=new praises();
                            sd.setObjectIDs(object.get(0).getObjectIDs());
                            sd.addObjectID(objectId);
                            sd.update(object.get(0).getObjectId(),new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                }
                            });
                        }else if(object.size()==0){
                            //服务器没有找个人的点赞数据，则应该增加
                            final praises sd=new praises();
                            sd.setUserID(userName);
                            sd.addObjectID(objectId);
                            sd.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {
                                }
                            });
                        }
                    }else {

                    }
                }
            });
        }
    }

    @Override
    public boolean isDynamicPraiseed(String objectId, String userName) {
        SharedPreferences preferences=AppApplication.getContext().getSharedPreferences(userName+"_praises",Context.MODE_PRIVATE);
        return preferences.getInt(objectId, 0) == 1;
    }

    public interface praiseAlumniCircle{
        void becomePraised();   //未点赞变成点赞状态
    }


}
